package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.dto.SysUserDto;
import com.cmti.skeleton.entity.SysUser;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.jdbc.SQL;

public class SysUserSqlProvider {

    public String insertSelective(SysUser record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("sys_user");

        if (record.getAccount() != null) {
            sql.VALUES("ACCOUNT", "#{account,jdbcType=VARCHAR}");
        }

        if (record.getPassword() != null) {
            sql.VALUES("PASSWORD", "#{password,jdbcType=VARCHAR}");
        }

        if (record.getName() != null) {
            sql.VALUES("NAME", "#{name,jdbcType=VARCHAR}");
        }

        if (record.getSex() != null) {
            sql.VALUES("SEX", "#{sex,jdbcType=TINYINT}");
        }

        if (record.getDeptId() != null) {
            sql.VALUES("DEPT_ID", "#{deptId,jdbcType=BIGINT}");
        }

        if (record.getCertType() != null) {
            sql.VALUES("CERT_TYPE", "#{certType,jdbcType=INTEGER}");
        }

        if (record.getCertNo() != null) {
            sql.VALUES("CERT_NO", "#{certNo,jdbcType=VARCHAR}");
        }

        if (record.getTel() != null) {
            sql.VALUES("TEL", "#{tel,jdbcType=VARCHAR}");
        }

        if (record.getMobile() != null) {
            sql.VALUES("MOBILE", "#{mobile,jdbcType=VARCHAR}");
        }

        if (record.getEmail() != null) {
            sql.VALUES("EMAIL", "#{email,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.VALUES("CREATE_TIME", "#{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getExpTime() != null) {
            sql.VALUES("EXP_TIME", "#{expTime,jdbcType=TIMESTAMP}");
        }

        if (record.getStatus() != null) {
            sql.VALUES("STATUS", "#{status,jdbcType=INTEGER}");
        }

        if (record.getLoginNum() != null) {
            sql.VALUES("LOGIN_NUM", "#{loginNum,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.VALUES("REMARK", "#{remark,jdbcType=VARCHAR}");
        }

        return sql.toString();
    }

    public String updateByPrimaryKeySelective(SysUser record) {
        SQL sql = new SQL();
        sql.UPDATE("sys_user");

        if (record.getAccount() != null) {
            sql.SET("ACCOUNT = #{account,jdbcType=VARCHAR}");
        }

        if (record.getPassword() != null) {
            sql.SET("PASSWORD = #{password,jdbcType=VARCHAR}");
        }

        if (record.getName() != null) {
            sql.SET("NAME = #{name,jdbcType=VARCHAR}");
        }

        if (record.getSex() != null) {
            sql.SET("SEX = #{sex,jdbcType=TINYINT}");
        }

        if (record.getDeptId() != null) {
            sql.SET("DEPT_ID = #{deptId,jdbcType=BIGINT}");
        }

        if (record.getCertType() != null) {
            sql.SET("CERT_TYPE = #{certType,jdbcType=INTEGER}");
        }

        if (record.getCertNo() != null) {
            sql.SET("CERT_NO = #{certNo,jdbcType=VARCHAR}");
        }

        if (record.getTel() != null) {
            sql.SET("TEL = #{tel,jdbcType=VARCHAR}");
        }

        if (record.getMobile() != null) {
            sql.SET("MOBILE = #{mobile,jdbcType=VARCHAR}");
        }

        if (record.getEmail() != null) {
            sql.SET("EMAIL = #{email,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.SET("CREATE_TIME = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getExpTime() != null) {
            sql.SET("EXP_TIME = #{expTime,jdbcType=TIMESTAMP}");
        }

        if (record.getStatus() != null) {
            sql.SET("STATUS = #{status,jdbcType=INTEGER}");
        }

        if (record.getLoginNum() != null) {
            sql.SET("LOGIN_NUM = #{loginNum,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.SET("REMARK = #{remark,jdbcType=VARCHAR}");
        }

        sql.WHERE("ID = #{id,jdbcType=BIGINT}");

        return sql.toString();
    }

    public String selectListByDto(SysUserDto dto) {
        SQL sql = new SQL()
                .SELECT("u.*, d.NAME as deptName")
                .FROM("sys_user u")
                .LEFT_OUTER_JOIN("sys_dept d ON u.DEPT_ID = d.ID")
                .WHERE("u.ACCOUNT != 'admin'");
        //账号查
        if (StringUtil.isNotEmpty(dto.getAccount())) {
            // 双向模糊匹配
            dto.setAccount("%" + dto.getAccount() + "%");
            sql.WHERE("u.ACCOUNT LIKE #{account}");
        }
        //用户名
        if (StringUtil.isNotEmpty(dto.getName())) {
            // 双向模糊匹配
            dto.setName("%" + dto.getName() + "%");
            sql.WHERE("u.NAME LIKE #{name}");
        }
        //部门ID查
        if (dto.getDeptId() != null && dto.getDeptId() > 0) {
            sql.WHERE("u.DEPT_ID = #{deptId}");
        }
        //状态
        if (dto.getStatus() != null && dto.getStatus() >= 0) {
            sql.WHERE("u.STATUS = #{status}");
        }

        //排序字段
        if (StringUtil.isNotEmpty(dto.getOrder()) && StringUtil.isNotEmpty(dto.getSort())) {
            sql.ORDER_BY(dto.getSort() + " " + dto.getOrder());
        } else {
            sql.ORDER_BY("u.ID asc");
        }

        return sql.toString();
    }

    public String insertUserRoles(SysUserDto dto) {
        StringBuilder sb = new StringBuilder("INSERT INTO sys_user_role(ROLE_ID,USER_ID) VALUES");
        long uid = dto.getId();
        String[] rids = dto.getRoleId();
        for (int i = 0; i < rids.length; i++) {
            sb.append("(#{roleId[").append(i).append("], javaType=String}, #{id}),");
        }
        sb.deleteCharAt(sb.length() - 1); // 移除最后一个,
        return sb.toString();
    }

    public static void main(String[] args) {
        SysUserSqlProvider sp = new SysUserSqlProvider();
        SysUserDto dto = new SysUserDto();
//        dto.setAccount("ad");
//        dto.setDeptId(1);
//        dto.setSort("name");
//        dto.setOrder("asc");
//        System.out.println(sp.selectListByDto(dto));
        dto.setId(1L);
        dto.setRoleId(new String[]{"3", "4", "5"});
        System.out.println(sp.insertUserRoles(dto));
    }

}
